package com.alibaba.basic.controller;


import com.alibaba.basic.dto.PhoneRegisterDto;
import com.alibaba.basic.service.VerifyCodeService;
import com.alibaba.basic.uitl.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;

/**
 * 验证码controller
 */
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private VerifyCodeService verifyCodeService;

    /**
     * 获取图形验证码
     *  base64ImageStr
     */
    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult imageCode(@PathVariable("imageCodeKey") String imageCode) throws IOException {
        String base64ImageStr = verifyCodeService.imageCode(imageCode);
        return AjaxResult.me().success(base64ImageStr);

    }

    /**
     * 获取手机验证码
     *  base64ImageStr
     */
    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody PhoneRegisterDto dto) throws IOException {
        verifyCodeService.smsCode(dto);
        return AjaxResult.me().success();

    }

    /**
     * 手机号注册接口
     *  base64ImageStr
     */
    @PostMapping("/phoneRegister")
    public AjaxResult phoneRegister(@RequestBody PhoneRegisterDto dto) throws IOException {
        verifyCodeService.phoneRegister(dto);
        return AjaxResult.me().success();

    }

    /**
     * 邮箱获取验证码
     */

    @GetMapping("/sendEmailCode/{email}")
    public AjaxResult sendEmailCode(@PathVariable("email") @Valid String email){
        String UUIDEmail = verifyCodeService.sendEmailCode(email);
        return AjaxResult.me().setResultObj(UUIDEmail).setSuccess(true);
    }
}
